package droid.ru;

import java.io.InputStreamReader;
import java.lang.reflect.Array;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Iterator;

import org.htmlcleaner.CleanerProperties;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;

public class MenuHelper {
	
	// List of List's of Strings. Each ArrayList representa's a week day. It's String's are menu items.
	private static ArrayList<ArrayList<String>> menu;
	// TagNode object used to iterate through HTML tags and text 
	private static TagNode node;
	// The XPath that drives HtmlCleaner to where we want
	private static final String foodXPath = "//div[@id='cardapio']/div";
	
	
	public static ArrayList<ArrayList<String>> fetchWeekData() {
		try {
			
			String menuUrl = "http://www.ufrgs.br/ufrgs/ru";

			// Initialize the HtmolCleaner object
			HtmlCleaner cleaner = new HtmlCleaner();
			CleanerProperties props = cleaner.getProperties();
			props.setAllowHtmlInsideAttributes(true);
			props.setAllowMultiWordAttributes(true);
			props.setRecognizeUnicodeChars(true);
			props.setOmitComments(true);

			// Open a connection to the desired URL
			URL url = new URL(menuUrl);
			URLConnection conn = url.openConnection();

			// Use the cleaner to "clean" the HTML and return it as a TagNode
			// object
			node = cleaner.clean(new InputStreamReader(conn.getInputStream()));

			Object[] pNodes = node.evaluateXPath(foodXPath);
			
			if (pNodes.length > 0) {
				menu = new ArrayList<ArrayList<String>>();
				
				// Monday
				TagNode info_node = (TagNode) pNodes[0];
				System.out.println("\n\nProcessing monday\n");
				menu.add(processNode(info_node));
				System.out.flush();
				// Tuesday
				info_node = (TagNode) pNodes[1];
				System.out.println("\n\nProcessing tuesday\n");
				menu.add(processNode(info_node));
				System.out.flush();
				// Wednesday				
				info_node = (TagNode) pNodes[2];
				System.out.println("\n\nProcessing wednesday\n");
				menu.add(processNode(info_node));
				System.out.flush();
				// Thursday
				info_node = (TagNode) pNodes[3];
				System.out.println("\n\nProcessing thursday\n");
				menu.add(processNode(info_node));
				System.out.flush();
				// Friday
				info_node = (TagNode) pNodes[4];
				System.out.println("\n\nProcessing friday\n");
				menu.add(processNode(info_node));
				System.out.flush();
			}
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return menu;
	}
	
	private static ArrayList<String> processNode(TagNode node)
	{
		ArrayList<String> daylyMenu = new ArrayList<String>();
		
		try {
			Iterator iter = node.getChildren().iterator();
			for (int i = 0; i < node.getChildren().size(); i++)
			{
				
				String info = iter.next().toString().trim();
				System.out.println(info);
				if(!info.equals("br") && !info.contains("h3") && !info.equals(""))
				{
					daylyMenu.add(info);
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		return daylyMenu;
	}
	

}